package lib.self.utils;

import android.content.Context;
import android.widget.Toast;

/**
 * description: Toast工具类 解决Toast重复显示
 * author: Steven Xiao
 * date: 2017/3/7
 */
public class ToastUtil {
    private static String mOld;
    protected static Toast mToast = null;
    private static long mPreTime = 0;//上次Toast显示的时间
    private static long mNowTime = 0;//当前时间

    public static void showToast(Context context, String text) {
        if (mToast == null) {
            mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
            mToast.show();
            mPreTime = System.currentTimeMillis();
        } else {
            mNowTime = System.currentTimeMillis();
            if (text.equals(mOld)) {
                if (mNowTime - mPreTime > Toast.LENGTH_SHORT) {
                    mToast.show();
                }
            } else {
                mOld = text;
                mToast.setText(text);
                mToast.show();
            }
        }
        mPreTime = mNowTime;
    }

    public static void showToast(Context context, int resId) {
        showToast(context, context.getString(resId));
    }
}
